/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: dog_error.cpp
 *
 * Purpose: error helper function implementation
 *
 * Developer:
 *   wen.gu , 2020-08-19
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "dog_errors.h"
#include "dog_json.h"

namespace dog
{

/******************************************************************************
 **    MACROS
 ******************************************************************************/
#define ERR_CASE(err) case err: ret = #err; break
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
const char* ErrorStr(DogErrc errc)
{
    char const* ret = "unknown";
    switch (errc)
    {
    ERR_CASE(DogErrc::OK);
    ERR_CASE(DogErrc::Fail);
    ERR_CASE(DogErrc::BadParameter);
    ERR_CASE(DogErrc::NoInit);
    ERR_CASE(DogErrc::Eof);
    ERR_CASE(DogErrc::NotImplemented);
    ERR_CASE(DogErrc::NotFound);
    ERR_CASE(DogErrc::Undefined);
    ERR_CASE(DogErrc::InvaildName);
    ERR_CASE(DogErrc::InsufficientResources);
    ERR_CASE(DogErrc::InvalidStatus);
    ERR_CASE(DogErrc::OutOfRange);
    ERR_CASE(DogErrc::UnsupportedType);
    ERR_CASE(DogErrc::Timeout);
    ERR_CASE(DogErrc::BadAddress);
    ERR_CASE(DogErrc::ConnectionAborted); /**< connection aborted(ECONNABORTED) */
    ERR_CASE(DogErrc::ConnectionAlreadyInProgress); /**< connection already in progress(EALREADY) */
    ERR_CASE(DogErrc::ConnectionRefused); /**< connection refused(ECONNREFUSED) */
    ERR_CASE(DogErrc::ConnectionReset); /**< connection reset(ECONNRESET) */
    ERR_CASE(DogErrc::BadMessage);
    ERR_CASE(DogErrc::OperationCanceled); /**< operation canceled(ECANCELED) */
    ERR_CASE(DogErrc::OperationInProgress); /**< operation in progress(EINPROGRESS) */
    ERR_CASE(DogErrc::OperationNotPermitted); /**< operation not permitted(EPERM) */
    ERR_CASE(DogErrc::OperationNotSupported); /**< operation not supported(EOPNOTSUPP) */
    ERR_CASE(DogErrc::OperationWouldBlock); /**< operation would block(EWOULDBLOCK) */
    ERR_CASE(DogErrc::OutOfMemory);
    ERR_CASE(DogErrc::AlreadyExisted);
    ERR_CASE(DogErrc::ResourceIsBusy);
    default: break;
    }

    return ret;
}

/**
 * json:
 * {
 *    code: xxx,   //Integer
      description: xxx  //string
 * }
 */
void Error2Json(DogErrc err, Value& val)
{
    val["code"] = (int32_t)err;
    val["description"] = ErrorStr(err);
}

/**
 * json:
 * {
 *     code: xxx //integer
 * }
 *
 * here just need error code in json value
 */
DogErrc Json2Error(const Value& val)
{
    DogErrc ret = DogErrc::Undefined;

    if (val.isMember("code"))
    {
        ret = (DogErrc)val["code"].asInt();
    }

    return ret;
}

/**
 * json:
 * {
 *     description: xxx  //string
 * }
 *
 * here just need error description in json value
 */
std::string Json2ErrorStr(const Value& val)
{
    if (val.isMember("description"))
    {
        return val["description"].asString();
    }

    return "";
}

} /** namespace dog */
